package com.fastloan.mgt.product.flow.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.expert.dao.ExpertMapper;
import com.fastloan.mgt.expert.model.vo.ExpertVO;
import com.fastloan.mgt.expert.service.ExpertService;
import com.fastloan.mgt.processcontrol.base.service.ProcessFunctionService;
import com.fastloan.mgt.product.basics.model.Product;
import com.fastloan.mgt.product.basics.service.ProductService;
import com.fastloan.mgt.product.expection.ProductApproveException;
import com.fastloan.mgt.product.flow.dao.ProductFlowMapper;
import com.fastloan.mgt.product.flow.enums.ProductFunctionGroup;
import com.fastloan.mgt.product.flow.model.ProductFlow;
import com.fastloan.mgt.product.flow.service.ProductFlowService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.org.model.CmuOrg;
import com.fastloan.safe.mgt.org.model.vo.OrgTreeVo;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.fastloan.safe.mgt.user.model.vo.CmuUserVo;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fastloan.mgt.product.enums.ProductEnum.PUBLISHED;
import static com.fastloan.mgt.product.enums.ProductEnum.UNPUBLISHED;
import static com.fastloan.safe.core.param.RoleTypeEnum.EXPERT;

/**
 * @author admins
 *         createTime 2017/11/08
 */
@Service
public class ProductFlowServiceImpl extends BasicServiceImpl<ProductFlow> implements ProductFlowService {
    @Autowired
    private ProductFlowMapper productFlowMapper;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProcessFunctionService processFunctionService;

    @Autowired
    private ExpertService expertService;

    @Override
    @Transactional
    public Integer insertList(List<ProductFlow> list) {
        String productId = list.stream().findAny().get().getProductId();
        Product product = productService.selectByPrimaryKey(productId);
        ProductFlow productFlow = new ProductFlow();
        productFlow.setProductId(productId);
        productFlow.setStatus(UNPUBLISHED.getType());
        if (product != null && !product.getStatus().equals(UNPUBLISHED.getType())) {
            select(productFlow).forEach(flow -> {
                flow.setStatus(PUBLISHED.getType());
                updateByPrimaryKeySelective(flow);
            });
        }
        delete(productFlow);
        return productFlowMapper.insertList(list);
    }

    @Override
    public Map<String, Object> getProcessFunction(String productId) {
        Map<String, Object> map = new HashMap<>();
        map.put("default", processFunctionService.getAllProcess(ProductFunctionGroup.GROUP_ONE));
        if (!StringUtils.isBlank(productId)) {
            ProductFlow productFlow = new ProductFlow();
            productFlow.setProductId(productId);
            productFlow.setStatus(UNPUBLISHED.getType());
            map.put("storage", select(productFlow));
        }
        return map;
    }

    @Override
    public Integer getProductFlowCount(String productId) {
        Example example = new Example(ProductFlow.class);
        example.createCriteria().andEqualTo("productId", productId)
                .andEqualTo("status", UNPUBLISHED.getType());
        return selectCountByExample(example);
    }

    @Override
    public boolean ifHaveApprove(String productId) {
        Example example = new Example(ProductFlow.class);
        example.createCriteria().andEqualTo("productId", productId)
                .andEqualTo("status", UNPUBLISHED.getType());
        List<ProductFlow> productFlowList = selectByExample(example);
        return productFlowList.stream().anyMatch(productFlow -> productFlow.getProcessClass().equals("ApplicationApproveTemplateServiceImpl"));
    }

    @Override
    public Integer pageFlow(List<ProductFlow> productFlowList) throws ProductApproveException {
        long count = productFlowList.stream()
                .filter(productFlow -> productFlow.getProcessClass().equals("ApplicationApproveTemplateServiceImpl"))
                .count();
        List<ExpertVO> expertVOS =  expertService.queryAllExpert(ManagerKit.getTopOrgId());
        if (count > 0 && expertVOS.isEmpty()) {
            throw new ProductApproveException("未找到审批专家");
        }
        return insertList(productFlowList);
    }
}
